<?php
/**
 * Created by PhpStorm.
 * User: nfangxu
 * Date: 19-1-13
 * Time: 下午5:46
 */

namespace App\Services;


use App\Models\Customer;
use Illuminate\Http\Request;

class Api
{
    public static function success($data = [], $msg = "ok", $code = 200)
    {
        return self::json([
            'code' => $code,
            'msg' => __($msg),
            'data' => $data
        ]);
    }

    public static function failure($code, $msg = "failure", $data = [])
    {
        return self::json([
            'code' => $code,
            'msg' => __($msg),
            'data' => $data
        ]);
    }

    public static function json($data)
    {
        return response()->json($data);
    }

    public static function token(Customer $customer)
    {
        $customer->touch();
        return [
            'token_type' => 'Bearer',
            'expires_in' => 31536000,
            'access_token' => $customer->createToken($customer->username)->accessToken,
            'refresh_token' => null,
        ];
    }

    public static function msg($key, $data = [])
    {
        return static::json([
            'code' => (int)trans($key . "_code"),
            'msg' => trans($key),
            'data' => $data,
        ]);
    }
}
